/*
 * $Id: ActFileTransferDetailsDAO.java,v 1.11 2009/07/23 21:05:03 afaichun Exp $
 * 
 * Copyright (C) 2006 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.common.database.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ge.healthcare.autosc.common.ASCUtil;
import com.ge.healthcare.autosc.common.database.pojo.ActFileTransferDetails;

/**
 * @author 212042764
 * 
 */
public class ActFileTransferDetailsDAO extends BaseDAO implements
		IFileTransferDetailsDAO {
	

	/**
	 * Eclipse generated
	 */
	private static final long serialVersionUID = -610818205817690707L;

	public ActFileTransferDetailsDAO() {
		super(ActFileTransferDetails.TABLE_NAME);
	}

	/**
	 * @see com.ge.healthcare.autosc.common.database.dao.IFileTransferDetailsDAO#getFileTransferDetailsById(java.lang.Long)
	 */
	public ActFileTransferDetails getFileTransferDetailsById(Long fileTransferId) {
		return (ActFileTransferDetails) this.findObjectByID(ActFileTransferDetails.COL_FT_ID,
				fileTransferId);
	}

	/**
	 * @see com.ge.healthcare.autosc.common.database.dao.IFileTransferDetailsDAO#getFileTransferLstByRequestId(java.lang.Long)
	 */
	public List<ActFileTransferDetails> getFileTransferLstByRequestId(
			Long requestId) {
		List<?> rtvdFTDetails = this.findByProperty(ActFileTransferDetails.COL_REQUEST_ID, requestId);
		if (ASCUtil.isNullOrEmptyList(rtvdFTDetails)) {
			return null;
		}
		List<ActFileTransferDetails> fileTransferDetailsLst = new ArrayList<ActFileTransferDetails>();
		for (Object ftDetail : rtvdFTDetails) {
			fileTransferDetailsLst.add((ActFileTransferDetails) ftDetail);
		}
		return fileTransferDetailsLst;
	}

	/**
	 * Delete file transfer record by request Id and direction.
	 * @param requestId
	 * @param direction
	 * @return list of ActFileTransferDetails that has been deleted.
	 */
	public List<ActFileTransferDetails> deleteByRequestIdAndDirection(
			Long requestId, String direction) {
		Map<String, Object> whereClause = new HashMap<String, Object>();
		whereClause.put(ActFileTransferDetails.COL_REQUEST_ID, requestId);
		whereClause.put(ActFileTransferDetails.COL_DIRECTION, direction);
		List<?> rtvdFTDetails = this.findByProperties(whereClause);
		if (ASCUtil.isNullOrEmptyList(rtvdFTDetails)) {
			return null;
		}
		List<ActFileTransferDetails> fileTransferDetailsLst = new ArrayList<ActFileTransferDetails>();
		for(Object object: rtvdFTDetails) {
			ActFileTransferDetails fileTransferDetail = (ActFileTransferDetails)object;
			this.deleteByID(fileTransferDetail);
			fileTransferDetailsLst.add(fileTransferDetail);
		}
		return fileTransferDetailsLst;
	}

	/**
	 * The method returns external files which has been successfully transfered
	 * to the back office and belong to specified request.
	 * 
	 * @param requestId
	 * @return
	 */
	public ActFileTransferDetails getExternalFileTransferByRequestIdAndFileName(
			Long requestId, String fileName) {
		Map<String, Object> whereClause = new HashMap<String, Object>();
		whereClause.put(ActFileTransferDetails.COL_REQUEST_ID, requestId);
		whereClause.put(ActFileTransferDetails.COL_IS_INTERNAL, ActFileTransferDetails.EXTERNAL);
		whereClause.put(ActFileTransferDetails.COL_DIRECTION, UPLOAD_FROM_DEVICE);
		whereClause.put(ActFileTransferDetails.COL_STATUS, COMPLETED);
		whereClause.put(ActFileTransferDetails.COL_FILE_NAME, fileName);
		List<?> rtvdFTDetails = this.findByProperties(whereClause);
		if (ASCUtil.isNullOrEmptyList(rtvdFTDetails)) {
			return null;
		}
		return (ActFileTransferDetails)	rtvdFTDetails.get(0);
	}
	
	/**
	 * The method returns list of all external files which need to be
	 * transferred
	 * 
	 * @param requestId
	 * @return
	 */
	public List<ActFileTransferDetails> getExternalFileTransferLstByRequestId(
			Long requestId) {
		Map<String, Object> whereClause = new HashMap<String, Object>();
		whereClause.put(ActFileTransferDetails.COL_REQUEST_ID, requestId);
		whereClause.put(ActFileTransferDetails.COL_IS_INTERNAL, ActFileTransferDetails.EXTERNAL);
		whereClause.put(ActFileTransferDetails.COL_DIRECTION, UPLOAD_FROM_DEVICE);
		whereClause.put(ActFileTransferDetails.COL_STATUS, COMPLETED);
		List<?> rtvdFTDetails = this.findByProperties(whereClause);
		if (ASCUtil.isNullOrEmptyList(rtvdFTDetails)) {
			return null;
		}
		List<ActFileTransferDetails> fileTransferDetailsLst = new ArrayList<ActFileTransferDetails>();
		for (Object ftDetail : rtvdFTDetails) {
			fileTransferDetailsLst.add((ActFileTransferDetails) ftDetail);
		}
		return fileTransferDetailsLst;
	}
	
	/**
	 * Deletes all file transfer details stored for request id
	 * @param requestId
	 */
	public void deleteByRequestId(Long requestId){
		this.deleteByProperty(ActFileTransferDetails.COL_REQUEST_ID, requestId);
	}

	/**
	 * @see com.ge.healthcare.autosc.common.database.dao.BaseDAO#save(java.lang.Object)
	 */
	public void save(Object pojo) {
		super.save(pojo);
	}

	/**
	 * @see com.ge.healthcare.autosc.common.database.dao.IRequestTypeDAO#saveAlways(java.lang.Object)
	 */
	public void saveAlways(Object pojo) {
		super.save(pojo);
	}

	/**
	 * @see com.ge.healthcare.autosc.common.database.dao.BaseDAO#deleteByID(java.lang.Object)
	 */
	public void deleteByID(Object pojo) {
		super.deleteByID(pojo);
	}

	/**
	 * @see com.ge.healthcare.autosc.common.database.dao.BaseDAO#refresh(java.lang.Object)
	 */
	public void refresh(Object pojo) {
		super.refresh(pojo);
	}
}
